package com.shchuangzhi.smartguard.fragment;


import android.os.Bundle;
import android.app.Fragment;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.elvishew.xlog.XLog;
import com.shchuangzhi.smartguard.DividerItemDecoration;
import com.shchuangzhi.smartguard.LoginActivity;
import com.shchuangzhi.smartguard.NotifyMessageViewAdapter;
import com.shchuangzhi.smartguard.R;
import com.shchuangzhi.smartguard.core.Consts;
import com.shchuangzhi.smartguard.core.Job;
import com.shchuangzhi.smartguard.core.JobManager;
import com.shchuangzhi.smartguard.core.Notify;
import com.shchuangzhi.smartguard.core.NotifyMessage;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class Event extends Fragment implements FragmentNamed {

    private Handler handler;
    private RecyclerView recyclerView;
    private static final int MAX_CAPACITY = 100;
    private List<NotifyMessage> list = new ArrayList<>(MAX_CAPACITY);
    public Event() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_event, container, false);
        recyclerView = (RecyclerView)view.findViewById(R.id.event_message_recycler_view);
        LinearLayoutManager layout = new LinearLayoutManager(this.getActivity());
        layout.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layout);
        final NotifyMessageViewAdapter adapter = new NotifyMessageViewAdapter(list);
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(new DividerItemDecoration(this.getActivity(),
                DividerItemDecoration.VERTICAL_LIST));

        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                try {
                    NotifyMessage message = (NotifyMessage)msg.obj;
                    if (list.size() >= MAX_CAPACITY) {
                        List<NotifyMessage> tmp = list;
                        list = new ArrayList<>(MAX_CAPACITY);
                        list.addAll(tmp.subList(0,50));
                        adapter.reload(list);
                    }
                    list.add(0, message);
                    adapter.notifyDataSetChanged();
                }catch (Exception e) {
                    //keep main thread from interrupted
                    Log.e("SmartGuard-Log", "fatal", e);
                }
            }
        };

        final Job dataSyncJob = JobManager.get(Consts.JOB_DATA_SYNC);
        if (dataSyncJob != null) {
            dataSyncJob.addNotify(Consts.NOTIFY_DATA_SYC_VIEW_LIST, new Notify() {
                @Override
                public void notify(NotifyMessage message) {
                    handler.sendMessage(handler.obtainMessage(Consts.NOTIFY_DATA_SYC_VIEW_LIST, message));
                }
            });
        }

        return view;
    }

    @Override
    public String Name() {
        return Consts.EVENT_FRAGMENT_NAME;
    }

    @Override
    public Fragment Fragment() {
        return this;
    }
}
